<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-button type="primary" :icon="CirclePlus" @click="onEdit(0)">添加</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-date-picker class="ml15" v-model="form.issue_date" @change="onSelectDate" type="month" value-format="YYYY-MM" placeholder="请选择月份" />
            </el-col>
        </el-row>
        <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center" />
            <el-table-column label="姓名" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.labourer.name }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="发放周期" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.period }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="应发工资" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.pay }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="扣除项" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.deductions }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="实发工资" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.price }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="发放日期" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.payment_date }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="发放状态" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.status ? '已发放' : '未发放' }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="支付方式" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.method }}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160" align="center">
                <template #default="scope">
                    <el-button link type="primary" @click="onEdit(scope.row.id)" size="small">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>
</template>

<script lang="ts" setup>
import { CirclePlus } from '@element-plus/icons-vue'
import { ref,onMounted } from 'vue'
import http from '../../../util/request';
import router from '../../../router';
import { ElMessage } from 'element-plus';
const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',issue_date:'',page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})

interface User {
    id: number
    period: string
    pay: string
    deductions: string
    price: string
    payment_date: string
    status: string
    method: string
    remarks:string
}

const tableData = ref<User[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/labourer/wage',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onSelectDate = () => {
    onGetData()
}

const onEdit = (id: number) => {
    if(!form.value.issue_date){
        ElMessage.error('请先选择日期');
        return
    }
    router.push({ name: 'labourersPayEdit',params: { id: id ,date: form.value.issue_date }})
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}
onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>